package touchgestIT;

import gestIT.Token;

import java.util.Vector;


public class TouchToken extends Token {
	
	public PointTracking tracking;
	
	private Vector<TouchPoint>[] touches;
	private int touchCount;
	
	public TouchToken(PointTracking tracking){
		this(tracking,10);
	}
	
	
	@SuppressWarnings("unchecked")
	public TouchToken(PointTracking tracking, int touchCount){
		super();
		
		this.tracking = tracking;
		this.touchCount = touchCount;
		
		Vector<?>[] touchs = new Vector<?>[this.touchCount];
		this.touches = (Vector<TouchPoint>[])touchs;
		
		int i;
		for(i = 0; i < this.touches.length; i++){
			switch (this.tracking) {
			case Last:
				touches[i] = new Vector<TouchPoint>(1);
				touches[i].add(new TouchPoint());
				break;

			case Path:
				touches[i] = new Vector<TouchPoint>();
				break;
			
			case Previous:
				touches[i] = new Vector<TouchPoint>(2);
				touches[i].add(new TouchPoint());
				touches[i].add(new TouchPoint());
				break;	
			
			case StartLast:
				touches[i] = new Vector<TouchPoint>(2);
				touches[i].add(new TouchPoint());
				touches[i].add(new TouchPoint());
				break;	
				
			case StartPrevious:
				touches[i] = new Vector<TouchPoint>(3);
				touches[i].add(new TouchPoint());
				touches[i].add(new TouchPoint());
				touches[i].add(new TouchPoint());
				break;
			}
		}
	}
	
	public void Save (TouchEventArgs args){
		
		if (args.Id < 0 || args.Id >= touches.length)
			return;
		
		int tid = args.Id;
		switch (this.tracking) {
		case Last:
			touches[tid].set(0, new TouchPoint(args.point.X, args.point.Y));
			break;

		case Path:
			touches[tid].add(new TouchPoint(args.point.X, args.point.Y));
			break;
		
		case Previous:
			touches[tid].set(0, touches[tid].get(1));
			touches[tid].set(1, new TouchPoint(args.point.X, args.point.Y));
			break;	
		
		case StartLast:
			
			if (args.type == TouchType.Start){
				touches[tid].set(0, new TouchPoint(args.point.X, args.point.Y));
			}
			touches[tid].set(1, new TouchPoint(args.point.X, args.point.Y));
			break;	
			
		case StartPrevious:
			if (args.type == TouchType.Start){
				touches[tid].set(0, new TouchPoint(args.point.X, args.point.Y));
			}
			touches[tid].set(1, touches[tid].get(2));
			touches[tid].set(2, new TouchPoint(args.point.X, args.point.Y));
			break;	
		}		
	}
	
	public TouchPoint GetPosition(int touchId){
		if (touchId < 0 || touchId >= touches.length)
			return TouchPoint.ErrorPoint;
		
		switch (this.tracking) {
		case Last:
			return touches[touchId].get(0);

		case StartLast:	case Previous:
			return touches[touchId].get(1);
					
		case StartPrevious:
			return touches[touchId].get(2);
			
		case Path:
			if (touches[touchId].size() == 0)
				return TouchPoint.ErrorPoint;
			else
				return touches[touchId].get(touches[touchId].size()-1);
			
		default:
			return TouchPoint.ErrorPoint;
		}
		
	}
	
	public TouchPoint GetPreviousPosition(int touchId){
		if (touchId < 0 || touchId >= touches.length)
			return TouchPoint.ErrorPoint;
		
		switch (this.tracking) {

		case Previous:
			return touches[touchId].get(0);
					
		case StartPrevious:
			return touches[touchId].get(1);
			
		case Path:
			if (touches[touchId].size() <= 1)
				return TouchPoint.ErrorPoint;
			else
				return touches[touchId].get(touches[touchId].size()-2);
			
		default:
			return TouchPoint.ErrorPoint;
		}
	}
	
	public TouchPoint GetStartPosition(int touchId){
		if (touchId < 0 || touchId >= touches.length)
			return TouchPoint.ErrorPoint;
		
		switch (this.tracking) {

		case StartLast: case StartPrevious:
			return touches[touchId].get(0);
			
		case Path:
			if (touches[touchId].size() == 0)
				return TouchPoint.ErrorPoint;
			else
				return touches[touchId].get(0);
			
		default:
			return TouchPoint.ErrorPoint;
		}
	}
	
	public TouchPoint GetPositionAtTime(int touchId, int i){
		if (touchId < 0 || touchId >= touches.length)
			return TouchPoint.ErrorPoint;
		if (this.tracking != PointTracking.Path)
			return TouchPoint.ErrorPoint;
		if (i < 0 || i >= touches[touchId].size())
			return TouchPoint.ErrorPoint;
		return touches[touchId].get(i);
	}
	
	public int GetPathLength(int touchId){
		if (touchId < 0 || touchId >= touches.length)
			return -1;
		if (this.tracking != PointTracking.Path)
			return -1;
		return touches[touchId].size();
	}

}
